package com.yunzheng.controller;

import com.yunzheng.common.BaseResult;
import com.yunzheng.dto.IterationChangeDTO;
import com.yunzheng.dto.IterationPostDto;
import com.yunzheng.entity.Project;
import com.yunzheng.exception.ServiceException;
import com.yunzheng.service.ProjectIterationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.Duration;


/**
 * @author 李美琪
 * @date 2020/8/1 11:00
 */

@Api(tags = "迭代")
@RestController
@RequestMapping("/iteration")
public class ProjectIterationController {

    @Resource
    private ProjectIterationService projectIterationService;

    /**
     * 新建迭代
     *
     * /post -- authc
     */
    @ApiOperation(value = "新建迭代")
    @PostMapping("/post")
    public BaseResult iterationPost(@Valid @RequestBody IterationPostDto in){
        projectIterationService.iterationPost(in.getProjectSid(),in.getProjectName(),in.getProjectStatus(),in.getDescription(),in.getStartTime(),in.getFinishTime());
        return BaseResult.ok();
    }

    /**
     * 通过关键字搜索迭代
     *
     * /seek -- authc
     */
    @ApiOperation(value = "通过关键字搜索迭代")
    @GetMapping("/seek")
    public BaseResult iterationSeek(@RequestParam String forSeek, String projectId){
        return BaseResult.ok(projectIterationService.iterationSeek(forSeek, projectId));
    }

    /**
     * 迭代信息列表
     *
     * /show/list -- authc
     */
    @ApiOperation(value = "迭代信息列表")
    @GetMapping("/show/list")
    public BaseResult missionShowList(@RequestParam String projectSid){
        return BaseResult.ok(projectIterationService.iterationShowList(projectSid));
    }

    /**
     * 展示迭代信息
     *
     * /show -- authc
     */
    @ApiOperation(value = "展示迭代信息")
    @GetMapping("/show")
    public BaseResult missionShow(@RequestParam String projectId){
        return BaseResult.ok(projectIterationService.iterationShow(projectId));
    }

    /**
     * 修改迭代
     * /change -- authc
     */
    @ApiOperation(value = "修改迭代")
    @PutMapping("/change")
    public BaseResult missionChange(@Valid @RequestBody IterationChangeDTO in){
//        projectIterationService.iterationChange(in.getProjectId(),in.getProjectName(),in.getProjectManager(),in.getProjectStatus(),in.getDescription(),in.getStartTime(),in.getFinishTime());
        Project project = new Project();
        BeanUtils.copyProperties(in , project);
        long time = Duration.between(in.getStartTime(),in.getFinishTime()).toDays();
        if(time < 0) {
            throw new ServiceException("结束时间早于开始时间");
        }
//        project.setFinishTime(LocalDateTime.now()).setCreateTime(LocalDateTime.now());
        projectIterationService.updateById(project);
        return BaseResult.ok();
    }

    /**
     * 删除迭代
     * /remove -- authc
     */
    @ApiOperation(value = "删除迭代")
    @DeleteMapping("/remove")
    public BaseResult missionRemove(@RequestParam String projectId){
//        projectIterationService.iterationRemove(projectId);
        projectIterationService.removeById(projectId);
        return BaseResult.ok();
    }

}
